$(function(){
    checkRemember();
    $("#vcodeImage").attr("src",basePath+"loginVcode");
    $("#vcodeImage").click(function(){
        console.log("更换验证码");
        var src = basePath+"loginVcode";
        $(this).attr("src",src+"?d="+new Date().getTime());
    })

    $("#logSubmitButton").click(function(){
        doLogin();
    });

    $("#goAlipayLoginButton").click(function(){
        window.location = basePath+"loginAlipayPage";
    });


    /* 登录框enter事件 */
    $(document).keypress(function(e){
        if(e.keyCode == "13"){
            if($("#userName").is(":focus")){
                $("#password").focus();
            }else if($("#password").is(":focus")){
                $("#vcode").focus();
            }else if($("#vcode").is(":focus")){
             doLogin();
             }
        }
    });

    //判断是否顶层页面
    goLogin();
});

//登录校验
function checkForm(){
    var userName=$.trim($("#userName").val());
    var password=$.trim($("#password").val());
    var vcode=$.trim($("#vcode").val());
    if(userName.length==0){
        $("#loginwarning").html("<span style='color:red'>请填写登录账号!</span>");
        return false;
    }
    if(password.length==0){
        $("#loginwarning").html("<span style='color:red'>请输入登录密码!</span>");
        return false;
    }
    if(vcode.length==0){
     $("#loginwarning").html("<span style='color:red'>请输入图片验证码!</span>");
     return false;
     }
    if(password.length<6||password.length>20){
        $("#loginwarning").html("<span style='color:red'>密码为6-20位!</span>");
        return false;
    }
    if(vcode.length!=4){
     $("#loginwarning").html("<span style='color:red'>请输入4位图片验证码!</span>");
     return false;
     }
    return true;
}

function checkRemember(){

}

function doLogin(){
    if(!checkForm())return;
    var passwordOld=$("#password").val();
    $("#password").val($.md5($("#password").val()).toUpperCase());
    var userName=$.trim($("#userName").val());
    var password=$.trim($("#password").val());
    var remember=$("#remember").attr("checked");
    $.ajax({
        url: basePath+"loginIn",
        type: "post",
        dataType: "json",
        data:$("#loginForm").serialize(),
        success: function (back) {
            if(!back){
                alert("登陆失败");
                $("#password").val(passwordOld);
                $("#vcodeImage").attr("src",basePath+"loginVcode"+"?d="+new Date().getTime());
                return;
            }
            if(!back.result){
                $("#loginwarning").empty().html("<span style='color:red'>"+back.message+"</span>");
                //$("#vcodeImage").trigger("click");
                $("#password").val(passwordOld);
                $("#vcodeImage").attr("src",basePath+"loginVcode"+"?d="+new Date().getTime());
                return;
            }
            $("#password").val(passwordOld);
            //登录成功跳转至首页
            window.location = basePath+"index";
        }
    });
}


function goLogin(){
    if(window!=top){
        top.location.href = basePath + "login";
    }
}